﻿
var ajaxRestAndHoliday = function (params) {
    $.ajax({
        url: "/Scheduling/GetRestAndHoliday",
        data: {
            limlt: params.data.limit,
            offset: params.data.offset
        },
        type: "post"
    }).done(function (data) {
        setTimeout(function () {
            params.success({
                total: data[1],
                rows: data[0]
            });
            params.complete();
        }, 1000);
    })
}

function DeleteRestAndHoildayObj() {
    RestAndHoildayObj = $('#tableAge').bootstrapTable('getSelections');
    if (RestAndHoildayObj.length == 1) {
        RestAndHoildayObjId = RestAndHoildayObj[0].Id;
    }
    else {
        RestAndHoildayObjId = 0;
    }
    if (RestAndHoildayObjId == 0) {
        swal({
            title: "提示",
            text: "-----------------请选择方案-----------------"
        });
    } else {
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的，我要删除！",
            cancelButtonText: "让我再考虑一下…",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function (isConfirm) {
            if (isConfirm) {
                var a1 = xhr({
                    url: "/Scheduling/DeleteRestAndHolidayById",
                    data: { id: RestAndHoildayObjId },
                    async: false,
                    method: 'post',
                    success: function (data) {
                        $('#tableAge').bootstrapTable('refresh');
                    }
                });
                swal("删除成功！", "您已经永久删除了这条信息。", "success");
            } else {
                swal("已取消", "您取消了删除操作！", "error");
            }
        });
    }
}

var HolidayObj;
var HolidayObjId;
var indexId = 0;
//////////////////////////////////////////
function MoreShow() {
    $("#HieID").val(0);
    $("#MoreShowDivTextName").val("");
    $("#MoreShowDivTextNameError").text("");
    ////////////////////////////////////////////////////
    $("#MoreShowDivTextMem").val("");
    $("#MoreShowDivTextMemError").text("");
    ////////////////////////////////////////////////////
    $('#MoreShowDivSundayMorning').iCheck('uncheck');
    $('#MoreShowDivMondayMorning').iCheck('uncheck');
    $('#MoreShowDivTuesdayMorning').iCheck('uncheck');
    $('#MoreShowDivWednesdayMorning').iCheck('uncheck');
    $('#MoreShowDivThursdayMorning').iCheck('uncheck');
    $('#MoreShowDivFridayMorning').iCheck('uncheck');
    $('#MoreShowDivSaturdayMorning').iCheck('uncheck');
    ////////////////////////////////////////////////////
    //$('#MoreSowItemTable').bootstrapTable('removeAll');
    //////////////////////////////////////////////////////
    //var a1 = xhr({
    //    url: "/Scheduling/GetYearString",
    //    data: {},
    //    async: false,
    //    method: 'post',
    //    success: function (data) {
    //        var objText = JSON.parse(data);
    //        var beginTime = objText + " -01-01";
    //        var endTime = objText + " -12-31";
    //        $("#TimeList").val(objText);
    //        TimeClick();
    //        //AddLoad(beginTime, endTime);
    //    }
    //});

    //进行ajax传值
    $.ajax({
        url: "/Scheduling/GetYearString",
        type: "post",
        async: false,
        dataType: "json",
        data: {},
        success: function (data) {
            //Info,Success,Warning,Error
            //var type = data.type;
            var objText = JSON.parse(data);
            var beginTime = objText + " -01-01";
            var endTime = objText + " -12-31";
            $("#TimeList").val(objText);
            TimeClick();
            //AddLoad(beginTime, endTime);

            //if (type == 1) {
            //    layer.msg(data.content, { icon: 6, time: 2000 },
            //        function () {
            //            var index = parent.layer.getFrameIndex(window.name);
            //            parent.layer.close(index);
            //        });

            //} else {
            //    layer.msg(data.content, { icon: 5, time: 2000 });
            //}
        }
    });



    $("#redNum").html("0");
    $("#Rest").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>休息</span>");
    $("#GoToWork").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>上班</span>");
    $("#EditSelectTime").hide();
    $("#SelectTime").show();
    loadTime();
    $('#MoreShowDiv').modal('show');
    $('#AddMessAger').attr("onclick", "AddMoreShowItem()");
}

function AddMoreShowItem() {
    var Err = 0;
    Err = 0;
    $("#MoreShowDivTextNameError").text("");
    ////////////////////////////////////////////////
    //名称
    var MoreShowDivTextName = $("#MoreShowDivTextName").val()
    if (IsNull(MoreShowDivTextName) != null) {
        $("#MoreShowDivTextNameError").text(IsNull(MoreShowDivTextName));
        Err = 1;
    }
    //内容
    var MoreShowDivTextMem = $("#MoreShowDivTextMem").val();
    /////////////////////////////////////////////////////////
    var Sunday = 0;
    var Monday = 0;
    var Tuesday = 0;
    var Wednesday = 0;
    var Thursday = 0;
    var Friday = 0;
    var Saturday = 0;
    if ($("#MoreShowDivSundayMorning").get(0).checked) {
        Sunday = Sunday + 1;
    }
    if ($("#MoreShowDivMondayMorning").get(0).checked) {
        Monday = Monday + 1;
    }
    if ($("#MoreShowDivTuesdayMorning").get(0).checked) {
        Tuesday = Tuesday + 1;
    }
    if ($("#MoreShowDivWednesdayMorning").get(0).checked) {
        Wednesday = Wednesday + 1;
    }
    if ($("#MoreShowDivThursdayMorning").get(0).checked) {
        Thursday = Thursday + 1;
    }
    if ($("#MoreShowDivFridayMorning").get(0).checked) {
        Friday = Friday + 1;
    }
    if ($("#MoreShowDivSaturdayMorning").get(0).checked) {
        Saturday = Saturday + 1;
    }
    ///////////////////////////////////////////////////////
    //var MoreDate = $('#MoreSowItemTable').bootstrapTable('getData');

    var redstateOne = "";
    $("#tbList").find("td").each(function () {
        if ($(this).attr("redstate") == "1") {
            redstateOne += "," + $(this).attr("Id");
        }
    });
    var redstateTwo = "";
    $("#tbList").find("td").each(function () {
        if ($(this).attr("redstate") == "2") {
            TimeList
            redstateTwo += "," + $(this).attr("Id");
        }
    });

    if (Err == 0) {

        $.ajax({
            type: "POST",
            url: "/Scheduling/SetItmeTable",
            data: {
                MoreShowDivTextName: MoreShowDivTextName,
                MoreShowDivTextMem: MoreShowDivTextMem,
                Sunday: Sunday,
                Monday: Monday,
                Tuesday: Tuesday,
                Wednesday: Wednesday,
                Thursday: Thursday,
                Friday: Friday,
                Saturday: Saturday,
                redstateOne: redstateOne,
                redstateTwo: redstateTwo
            },
            dataType: "json",
            async: false,
            success: function (data) {
                console.log(data);
                var type = data.type;
                if (type == 1) {
                    layer.msg(data.content, { icon: 6, time: 2000 },
                        function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });

                } else {
                    layer.msg(data.content, { icon: 5, time: 2000 });
                }
            },
            complete: function () { }
        });
        //var a1 = xhr({
        //    url: "/Scheduling/SetItmeTable",
        //    data: {
        //        MoreShowDivTextName: MoreShowDivTextName,
        //        MoreShowDivTextMem: MoreShowDivTextMem,
        //        Sunday: Sunday,
        //        Monday: Monday,
        //        Tuesday: Tuesday,
        //        Wednesday: Wednesday,
        //        Thursday: Thursday,
        //        Friday: Friday,
        //        Saturday: Saturday,
        //        redstateOne: redstateOne,
        //        redstateTwo: redstateTwo
        //    },
        //    async: false,
        //    method: 'post',
        //    success: function (data) {
        //        var objText = JSON.parse(data);
        //        swal({
        //            title: "恭喜",
        //            text: "-----------------恭喜添加成功-----------------"
        //        });
        //        $("#MoreShowDiv").modal('hide');
        //        $('#tableAge').bootstrapTable('refresh');
        //    }
        //});
    }
}

var EditMoreShowObj;
var EditMoreShowObjID;
function EditMoreShow() {
    $("#SelectTime").hide();
    $("#EditSelectTime").show();
    loadTime();
    $("#redNum").html("0");
    $("#Rest").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>休息</span>");
    $("#GoToWork").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>上班</span>");

    $('#AddMessAger').attr("onclick", "updateItem()");
    indexId = 0;
    EditMoreShowObj = $('#tableAge').bootstrapTable('getSelections');
    if (EditMoreShowObj.length == 1) {
        EditMoreShowObjID = EditMoreShowObj[0].Id;
        $("#HieID").val(EditMoreShowObjID);
    }
    else {
        EditMoreShowObjID = 0;
    }
    if (EditMoreShowObjID == 0) {
        swal({
            title: "提示",
            text: "-----------------请选择方案-----------------"
        });
    } else {
        $("#MoreShowDivTextName").val("");
        $("#MoreShowDivTextNameError").text("");
        ////////////////////////////////////////////////////
        $("#MoreShowDivTextMem").val("");
        $("#MoreShowDivTextMemError").text("");
        ////////////////////////////////////////////////////
        $('#MoreShowDivSundayMorning').iCheck('uncheck');
        $('#MoreShowDivMondayMorning').iCheck('uncheck');
        $('#MoreShowDivTuesdayMorning').iCheck('uncheck');
        $('#MoreShowDivWednesdayMorning').iCheck('uncheck');
        $('#MoreShowDivThursdayMorning').iCheck('uncheck');
        $('#MoreShowDivFridayMorning').iCheck('uncheck');
        $('#MoreShowDivSaturdayMorning').iCheck('uncheck');
        ////////////////////////////////////////////////////
        $('#MoreSowItemTable').bootstrapTable('removeAll');
        //////////////////////////////////////////////////////
        var a1 = xhr({
            url: "/Scheduling/GetRestAndHolidayById",
            data: {
                Id: EditMoreShowObjID
            },
            async: false,
            method: 'post',
            success: function (data) {
                var objText = JSON.parse(data);
                $("#MoreShowDivTextName").val(objText.TextName);
                $("#MoreShowDivTextMem").val(objText.TextMem);
            }
        });

        var a1 = xhr({
            url: "/Scheduling/GetRestAndHolidayOfficialHolidayById",
            data: {
                Id: EditMoreShowObjID
            },
            async: false,
            method: 'post',
            success: function (data) {
                var objText = JSON.parse(data);
                if (objText.Sunday == 1) {
                    $('#MoreShowDivSundayMorning').iCheck('check');
                } else {
                    $('#MoreShowDivSundayMorning').iCheck('uncheck');
                }
                /////////////////////////////////
                if (objText.Monday == 1) {
                    $('#MoreShowDivMondayMorning').iCheck('check');
                } else {
                    $('#MoreShowDivMondayMorning').iCheck('uncheck');
                }
                //////////////////////////////////////
                if (objText.Tuesday == 1) {
                    $('#MoreShowDivTuesdayMorning').iCheck('check');
                } else {
                    $('#MoreShowDivTuesdayMorning').iCheck('uncheck');
                }
                ///////////////////////////////////////
                if (objText.Wednesday == 1) {
                    $('#MoreShowDivWednesdayMorning').iCheck('check');
                } else {
                    $('#MoreShowDivWednesdayMorning').iCheck('uncheck');
                }
                ///////////////////////////////////////////
                if (objText.Thursday == 1) {
                    $('#MoreShowDivThursdayMorning').iCheck('check');
                } else {
                    $('#MoreShowDivThursdayMorning').iCheck('uncheck');
                }
                ////////////////////////////////////////////
                if (objText.Friday == 1) {
                    $('#MoreShowDivFridayMorning').iCheck('check');
                } else {
                    $('#MoreShowDivFridayMorning').iCheck('uncheck');
                }
                //////////////////////////////////////////////////
                if (objText.Saturday == 1) {
                    $('#MoreShowDivSaturdayMorning').iCheck('check');
                } else {
                    $('#MoreShowDivSaturdayMorning').iCheck('uncheck');
                }
            }
        });

        //////////////////////////////////////////////////////
        var a1 = xhr({
            url: "/Scheduling/GetYearString",
            data: {},
            async: false,
            method: 'post',
            success: function (data) {
                var objText = JSON.parse(data);
                var beginTime = objText + " -01-01";
                var endTime = objText + " -12-31";
                $("#TimeList").val(objText);
                TimeClick();
                //ShowLoad(EditMoreShowObjID, beginTime, endTime);
            }
        });
        $('#MoreShowDiv').modal('show');
    }
}
function updateItem() {
    var Err = 0;
    Err = 0;
    $("#MoreShowDivTextNameError").text("");
    ////////////////////////////////////////////////
    //名称
    var MoreShowDivTextName = $("#MoreShowDivTextName").val()
    if (IsNull(MoreShowDivTextName) != null) {
        $("#MoreShowDivTextNameError").text(IsNull(MoreShowDivTextName));
        Err = 1;
    }
    //内容
    var MoreShowDivTextMem = $("#MoreShowDivTextMem").val();
    /////////////////////////////////////////////////////////
    var Sunday = 0;
    var Monday = 0;
    var Tuesday = 0;
    var Wednesday = 0;
    var Thursday = 0;
    var Friday = 0;
    var Saturday = 0;
    if ($("#MoreShowDivSundayMorning").get(0).checked) {
        Sunday = Sunday + 1;
    }
    if ($("#MoreShowDivMondayMorning").get(0).checked) {
        Monday = Monday + 1;
    }
    if ($("#MoreShowDivTuesdayMorning").get(0).checked) {
        Tuesday = Tuesday + 1;
    }
    if ($("#MoreShowDivWednesdayMorning").get(0).checked) {
        Wednesday = Wednesday + 1;
    }
    if ($("#MoreShowDivThursdayMorning").get(0).checked) {
        Thursday = Thursday + 1;
    }
    if ($("#MoreShowDivFridayMorning").get(0).checked) {
        Friday = Friday + 1;
    }
    if ($("#MoreShowDivSaturdayMorning").get(0).checked) {
        Saturday = Saturday + 1;
    }
    var redstateOne = "";
    $("#tbList").find("td").each(function () {
        if ($(this).attr("redstate") == "1") {
            redstateOne += "," + $(this).attr("Id");
        }
    });
    var redstateTwo = "";
    $("#tbList").find("td").each(function () {
        if ($(this).attr("redstate") == "2") {
            redstateTwo += "," + $(this).attr("Id");
        }
    });
    ///////////////////////////////////////////////////////
    //var MoreDate = $('#MoreSowItemTable').bootstrapTable('getData');

    if (Err == 0) {
        var a1 = xhr({
            url: "/Scheduling/UpdateItem",
            data: {
                Id: EditMoreShowObjID,
                MoreShowDivTextName: MoreShowDivTextName,
                MoreShowDivTextMem: MoreShowDivTextMem,
                Sunday: Sunday,
                Monday: Monday,
                Tuesday: Tuesday,
                Wednesday: Wednesday,
                Thursday: Thursday,
                Friday: Friday,
                Saturday: Saturday,
                redstateOne: redstateOne,
                redstateTwo: redstateTwo
            },
            async: false,
            method: 'post',
            success: function (data) {
                var objText = JSON.parse(data);
                swal({
                    title: "恭喜",
                    text: "-----------------恭喜更新成功-----------------"
                });
                $("#MoreShowDiv").modal('hide');
                $('#tableAge').bootstrapTable('refresh');
            }
        });
    }
}
////////////////////////////////////////////

function GoToWorkClick() {
    if ($("#redNum").html() == 0) {
        $("#redNum").html("1");

        $("#GoToWork").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>上班......(使用中)</span>");
        $("#Rest").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>休息</span>");

    } else if ($("#redNum").html() == 1) {
        $("#redNum").html("0");

        $("#GoToWork").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>上班</span>");
        $("#Rest").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>休息</span>");

    } else if ($("#redNum").html() == 2) {
        $("#redNum").html("1");

        $("#GoToWork").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>上班......(使用中)</span>");
        $("#Rest").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>休息</span>");
    }
}
function RestClick() {
    if ($("#redNum").html() == 0) {
        $("#redNum").html("2");

        $("#Rest").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>休息......(使用中)</span>");
        $("#GoToWork").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>上班</span>");

    } else if ($("#redNum").html() == 1) {
        $("#redNum").html("2");

        $("#Rest").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>休息......(使用中)</span>");
        $("#GoToWork").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>上班</span>");

    } else if ($("#redNum").html() == 2) {
        $("#redNum").html("0");

        $("#Rest").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>休息</span>");
        $("#GoToWork").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>上班</span>");
    }
}
function ExitClick() {
    $("#redNum").html("0");
    $("#Rest").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>休息</span>");
    $("#GoToWork").html("<i class='fa fa-search'></i>&nbsp;&nbsp;<span class='bold'>上班</span>");
}
function loadTime() {
    //var obj = document.getElementById("TimeList");
    //obj.options.length = 0;
    //var a1 = xhr({
    //    url: "/Scheduling/GetDateList",
    //    data: {},
    //    async: false,
    //    method: 'post',
    //    success: function (data) {
    //        var objText = JSON.parse(data);
    //        for (var i = 0; i < objText.length; i++) {
    //            obj.options.add(new Option(objText[i].YearName, objText[i].Id)); //这个兼容IE与firefox 
    //        }
    //    }
    //});
}
function SetYear(year) {
    $("#TimeList").val(year);

    TimeClick();
}

function TimeClick() {
    var id = $("#HieID").val();
    var year = $("#TimeList").val();
    var beginTime = year + "-01-01";
    var endTime = year + "-12-31";
    if (id == 0) {
        AddLoad(beginTime, endTime);
    } else {
        EditLoad(id, beginTime, endTime);
    }
    $("#yearlist button").each(function () {
        $(this).attr("class", "btn btn-sm btn-success");
    });
    $("#RL_" + year).attr("class", "btn btn-sm btn-danger");
}

function AddLoad(beginTime, endTime) {
    var selectedMems = "";
    $.ajax({
        type: "POST",
        url: "/Scheduling/SearchPaiBan",
        data: { userIds: selectedMems, beginTime: beginTime, endTime: endTime },
        dataType: "json",
        success: function (data) {
            console.log(data);
            var $tbList = $("#tbList");
            $("#tbList").empty();
            $.each(data.data, function (index, val) {
                console.log(val);
                createTb(selectedMems, val, $tbList);
            })
        },
        complete: function () { }
    });
    function formatJsonDate(jsonDate) {
        console.log(jsonDate.substring(6));
        return (new Date(jsonDate.substring(0, 10)));
    };
    var createTb = function (selectedMems, data, $tbList) {

        var date = new Date(data.date);//formatJsonDate(data.date)
        console.log((date));
        var dataMonth = date.getMonth() + 1;
        var $tbItem = $("<div style='margin:5px 0px 5px 18px; height:150px;float:left;'></div>").appendTo($tbList);
        var $tb = $("<table id='tb-month' class='month-tb' memids='" + selectedMems + "'></table>").appendTo($tbItem);
        //创建表头
        var $thead = $("<thead></thead>").appendTo($tb);
        $("<tr><th colspan='7'>" + date.getFullYear() + "年" + dataMonth + "月</th></tr>").appendTo($thead);
        var $tHeadTr = $("<tr></tr>").appendTo($thead);
        for (var i = 1; i <= 7; i++) {
            switch (i) {
                case 1:
                    $("<th>一</th>").appendTo($tHeadTr);
                    break;
                case 2:
                    $("<th>二</th>").appendTo($tHeadTr);
                    break;
                case 3:
                    $("<th>三</th>").appendTo($tHeadTr);
                    break;
                case 4:
                    $("<th>四</th>").appendTo($tHeadTr);
                    break;
                case 5:
                    $("<th>五</th>").appendTo($tHeadTr);
                    break;
                case 6:
                    $("<th>六</th>").appendTo($tHeadTr);
                    break;
                case 7:
                    $("<th>日</th>").appendTo($tHeadTr);
                    break;
            }
        }
        //创建表body
        var $tbody = $("<tbody></tbody>").appendTo($tb);
        var day = 0;
        console.log(typeof (data.paiBanCalendarL));
        //tr
        for (var i = 1; i <= 6; i++) {
            if (day < data.paiBanCalendarL.length) {
                var $tr = $("<tr></tr>").appendTo($tbody);
                //td
                for (var j = 1; j <= 7; j++) {
                    var $td = $("<td></td>").appendTo($tr);
                    if (day < data.paiBanCalendarL.length && data.paiBanCalendarL[day].pB_Week == j) {
                        $td.addClass("day-td");
                        var spanText = day + 1;
                        var tdId = "td-" + date.getFullYear() + "-" + dataMonth + "-" + (day + 1);
                        $td.attr("id", tdId);
                        $td.attr("daytext", spanText);
                        //初始化td
                        initializeTd($td, spanText, data.paiBanCalendarL[day])
                        day++;
                    }
                }
            }
        }
    }
}

//获取法定假日模板
function GetHoliday() {
    var selectedMems = "";
    var year = $("#TimeList").val();
    $.ajax({
        type: "POST",
        url: "/Scheduling/GetHoliday",
        data: { Year: year },
        dataType: "json",
        success: function (data) {
            console.log(data);
            $("#tbList").empty();
            var $tbList = $("#tbList");
            $.each(data, function (index, val) {
                createTb(selectedMems, val, $tbList);
            })
        },
        complete: function () {

        }
    });
    function formatJsonDate(jsonDate) {
        return (new Date(parseInt(jsonDate.substr(6))));
    };
    var createTb = function (selectedMems, data, $tbList) {
        var date = new Date(formatJsonDate(data.Date));
        var dataMonth = date.getMonth() + 1;
        var $tbItem = $("<div style='margin:10px 0px 5px 20px; height:150px;float:left;'></div>").appendTo($tbList);
        var $tb = $("<table id='tb-month' class='month-tb' memids='" + selectedMems + "'></table>").appendTo($tbItem);
        //创建表头
        var $thead = $("<thead></thead>").appendTo($tb);
        $("<tr><th colspan='7'>" + date.getFullYear() + "年" + dataMonth + "月</th></tr>").appendTo($thead);
        var $tHeadTr = $("<tr></tr>").appendTo($thead);
        for (var i = 1; i <= 7; i++) {
            switch (i) {
                case 1:
                    $("<th>一</th>").appendTo($tHeadTr);
                    break;
                case 2:
                    $("<th>二</th>").appendTo($tHeadTr);
                    break;
                case 3:
                    $("<th>三</th>").appendTo($tHeadTr);
                    break;
                case 4:
                    $("<th>四</th>").appendTo($tHeadTr);
                    break;
                case 5:
                    $("<th>五</th>").appendTo($tHeadTr);
                    break;
                case 6:
                    $("<th>六</th>").appendTo($tHeadTr);
                    break;
                case 7:
                    $("<th>日</th>").appendTo($tHeadTr);
                    break;
            }
        }
        //创建表body
        var $tbody = $("<tbody></tbody>").appendTo($tb);
        var day = 0;
        //tr
        for (var i = 1; i <= 6; i++) {
            if (day < data.PaiBanCalendarL.length) {
                var $tr = $("<tr></tr>").appendTo($tbody);
                //td
                for (var j = 1; j <= 7; j++) {
                    var $td = $("<td></td>").appendTo($tr);
                    if (day < data.PaiBanCalendarL.length && data.PaiBanCalendarL[day].PB_Week == j) {
                        $td.addClass("day-td");
                        var spanText = day + 1;
                        var tdId = "td-" + date.getFullYear() + "-" + dataMonth + "-" + (day + 1);
                        $td.attr("id", tdId);
                        $td.attr("daytext", spanText);
                        $td.attr("redstate", data.PaiBanCalendarL[day].redstate);
                        $td.css({ "backgroundColor": data.PaiBanCalendarL[day].backgroundColor });
                        //初始化td
                        initializeTd($td, spanText, data.PaiBanCalendarL[day])

                        day++;
                    }
                }
            }
        }
    }

}

function ShowLoad(selectedMems, beginTime, endTime) {
    $("#tbList").empty();
    $.ajax({
        type: "POST",
        url: "/Scheduling/ShowSearchPaiBan",
        data: { userIds: selectedMems, beginTime: beginTime, endTime: endTime },
        dataType: "json",
        success: function (data) {
            console.log(data);
            var $tbList = $("#tbList");
            $("#tbList").empty();
            $.each(data, function (index, val) {
                createTb(selectedMems, val, $tbList);
            })
        },
        complete: function () {

        }
    });
    function formatJsonDate(jsonDate) {
        return (new Date(parseInt(jsonDate.substr(6))));
    };
    var createTb = function (selectedMems, data, $tbList) {
        var date = new Date(formatJsonDate(data.Date));
        var dataMonth = date.getMonth() + 1;
        var $tbItem = $("<div style='margin:10px 0px 5px 20px; height:150px;float:left;'></div>").appendTo($tbList);
        var $tb = $("<table id='tb-month' class='month-tb' memids='" + selectedMems + "'></table>").appendTo($tbItem);
        //创建表头
        var $thead = $("<thead></thead>").appendTo($tb);
        $("<tr><th colspan='7'>" + date.getFullYear() + "年" + dataMonth + "月</th></tr>").appendTo($thead);
        var $tHeadTr = $("<tr></tr>").appendTo($thead);
        for (var i = 1; i <= 7; i++) {
            switch (i) {
                case 1:
                    $("<th>一</th>").appendTo($tHeadTr);
                    break;
                case 2:
                    $("<th>二</th>").appendTo($tHeadTr);
                    break;
                case 3:
                    $("<th>三</th>").appendTo($tHeadTr);
                    break;
                case 4:
                    $("<th>四</th>").appendTo($tHeadTr);
                    break;
                case 5:
                    $("<th>五</th>").appendTo($tHeadTr);
                    break;
                case 6:
                    $("<th>六</th>").appendTo($tHeadTr);
                    break;
                case 7:
                    $("<th>日</th>").appendTo($tHeadTr);
                    break;
            }
        }
        //创建表body
        var $tbody = $("<tbody></tbody>").appendTo($tb);
        var day = 0;
        //tr
        for (var i = 1; i <= 6; i++) {
            if (day < data.PaiBanCalendarL.length) {
                var $tr = $("<tr></tr>").appendTo($tbody);
                //td
                for (var j = 1; j <= 7; j++) {
                    var $td = $("<td></td>").appendTo($tr);
                    if (day < data.PaiBanCalendarL.length && data.PaiBanCalendarL[day].PB_Week == j) {
                        $td.addClass("day-td");
                        var spanText = day + 1;
                        var tdId = "td-" + date.getFullYear() + "-" + dataMonth + "-" + (day + 1);
                        $td.attr("id", tdId);
                        $td.attr("daytext", spanText);
                        $td.attr("redstate", data.PaiBanCalendarL[day].redstate);
                        $td.css({ "backgroundColor": data.PaiBanCalendarL[day].backgroundColor });
                        //初始化td
                        initializeTd($td, spanText, data.PaiBanCalendarL[day])
                        day++;
                    }
                }
            }
        }
    }
}

function EditLoad(selectedMems, beginTime, endTime) {
    $("#tbList").empty();
    $.ajax({
        type: "POST",
        url: "/Scheduling/EditSearchPaiBan",
        data: { userIds: selectedMems, beginTime: beginTime, endTime: endTime },
        dataType: "json",
        success: function (data) {
            console.log(data);
            var $tbList = $("#tbList");
            $("#tbList").empty();
            $.each(data, function (index, val) {
                createTb(selectedMems, val, $tbList);
            })
        },
        complete: function () {

        }
    });
    function formatJsonDate(jsonDate) {
        return (new Date(parseInt(jsonDate.substr(6))));
    };
    var createTb = function (selectedMems, data, $tbList) {
        var date = new Date(formatJsonDate(data.Date));
        var dataMonth = date.getMonth() + 1;
        var $tbItem = $("<div style='margin:10px 0px 5px 20px; height:150px;float:left;'></div>").appendTo($tbList);
        var $tb = $("<table id='tb-month' class='month-tb' memids='" + selectedMems + "'></table>").appendTo($tbItem);
        //创建表头
        var $thead = $("<thead></thead>").appendTo($tb);
        $("<tr><th colspan='7'>" + date.getFullYear() + "年" + dataMonth + "月</th></tr>").appendTo($thead);
        var $tHeadTr = $("<tr></tr>").appendTo($thead);
        for (var i = 1; i <= 7; i++) {
            switch (i) {
                case 1:
                    $("<th>一</th>").appendTo($tHeadTr);
                    break;
                case 2:
                    $("<th>二</th>").appendTo($tHeadTr);
                    break;
                case 3:
                    $("<th>三</th>").appendTo($tHeadTr);
                    break;
                case 4:
                    $("<th>四</th>").appendTo($tHeadTr);
                    break;
                case 5:
                    $("<th>五</th>").appendTo($tHeadTr);
                    break;
                case 6:
                    $("<th>六</th>").appendTo($tHeadTr);
                    break;
                case 7:
                    $("<th>日</th>").appendTo($tHeadTr);
                    break;
            }
        }
        //创建表body
        var $tbody = $("<tbody></tbody>").appendTo($tb);
        var day = 0;
        //tr
        for (var i = 1; i <= 6; i++) {
            if (day < data.PaiBanCalendarL.length) {
                var $tr = $("<tr></tr>").appendTo($tbody);
                //td
                for (var j = 1; j <= 7; j++) {
                    var $td = $("<td></td>").appendTo($tr);
                    if (day < data.PaiBanCalendarL.length && data.PaiBanCalendarL[day].PB_Week == j) {
                        $td.addClass("day-td");
                        var spanText = day + 1;
                        var tdId = "td-" + date.getFullYear() + "-" + dataMonth + "-" + (day + 1);
                        $td.attr("id", tdId);
                        $td.attr("daytext", spanText);
                        $td.attr("redstate", data.PaiBanCalendarL[day].redstate);
                        $td.css({ "backgroundColor": data.PaiBanCalendarL[day].backgroundColor });
                        //初始化td
                        initializeTd($td, spanText, data.PaiBanCalendarL[day])

                        day++;
                    }
                }
            }
        }
    }
}
//初始化td
var initializeTd = function ($td, tdText, PaiBanCalendar) {
    $td.empty();
    //alert(tdText);
    //日期
    var $weekSpan = $("<span></span>").appendTo($td);
    $weekSpan.text(tdText);
    //冲突
    if (PaiBanCalendar.PB_ChongTu == 1) {
        var $ctDiv = $("<div></div>").appendTo($td);
        $ctDiv.addClass("ct-td-div");
        $ctDiv.text("数据冲突");
        $("<br />").appendTo($ctDiv);
        var $ctA = $("<a></a>").appendTo($ctDiv);
        $ctA.text("点击查看");
        $ctA.click(function () {
            alert("查看详情");
        });
    }
    else {
        var detailContent = "<div class='detail-span-content'>";
        $td.hover(function () {
            $(this).find(".popover-options").show();
        }, function () {
            $(this).find(".popover-options").hide();
        })
    }
    //添加td事件
    $td.removeAttr('onclick').unbind('click').click(function () {
        console.log($(this).attr("Id"));
        if ($("#redNum").html() == 1) {
            $("#" + $(this).attr("Id")).css({ "backgroundColor": "red" });
            $("#" + $(this).attr("Id")).attr("redstate", "1");
        }
        if ($("#redNum").html() == 2) {
            $("#" + $(this).attr("Id")).css({ "backgroundColor": "green" });
            $("#" + $(this).attr("Id")).attr("redstate", "2");
        }
        if ($("#redNum").html() == 0) {
            $("#" + $(this).attr("Id")).css({ "backgroundColor": "white" });
            $("#" + $(this).attr("Id")).attr("redstate", "0");
        }
    })
}

function AddYearClick() {
    var $tbList = $("#YearList");
    var $tbItem = $("<table id='2018' style='float:left;'><tr><td>2018年</td><td>删除</td></tr></table>").appendTo($tbList);
}